﻿using IcsSEMCtrl.Db;
using IcsSEMCtrl.Service.Logger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TKT.Lifter.CIM.Lifter.FluentScheduler
{
    class SchedulerJobManage
    {
        private static SchedulerJobManage schedulerJobManage;
        public static SchedulerJobManage getSchedulerJobManage()
        {
            if (schedulerJobManage == null)
            {
                schedulerJobManage = new SchedulerJobManage();
            }
            return schedulerJobManage;
        }

        //清除半年前的报警历史记录表
        public void ClearAlarmHistory()
        {
            DBConnection dBConnection = DBControl.GetConnection();
            string dateTime = DateTime.Now.AddMonths(-6).ToString("yyyy-MM-dd");
            string sqlString = $"DELETE FROM ALARMHISTORY WHERE CREATETIME < '{dateTime}'";
            int result = dBConnection.ExecuteUpdate(sqlString);
            if (result > 0)
            {
                LogHelper.loggerInfo($"清除报警历史记录表完成，共删除{result}条数据");
            }
            else
            {
                LogHelper.loggerInfo($"清除报警历史记录表0条");
            }
            DBControl.RemoveDBConection(dBConnection);
        }
        //清除半年前的物料历史记录表
        public void ClearMaterialHistory()
        {
            DBConnection dBConnection = DBControl.GetConnection();
            string dateTime = DateTime.Now.AddMonths(-6).ToString("yyyy-MM-dd");
            string sqlString = $"DELETE FROM MATERIALHISTORY WHERE COMPLETETIME < '{dateTime}'";
            int result = dBConnection.ExecuteUpdate(sqlString);
            if (result > 0)
            {
                LogHelper.loggerInfo($"清除物料历史记录表完成，共删除{result}条数据");
            }
            else
            {
                LogHelper.loggerInfo($"清除物料历史记录表0条");
            }
            DBControl.RemoveDBConection(dBConnection);
        }
        //清除半年前的指令历史记录表
        public void ClearCommandHistory()
        {
            DBConnection dBConnection = DBControl.GetConnection();
            string dateTime = DateTime.Now.AddMonths(-6).ToString("yyyy-MM-dd");
            string sqlString = $"DELETE FROM COMMANDHISTORY WHERE COMPLETETIME < '{dateTime}'";
            int result = dBConnection.ExecuteUpdate(sqlString);
            if (result > 0)
            {
                LogHelper.loggerInfo($"清除指令历史记录表完成，共删除{result}条数据");
            }
            else
            {
                LogHelper.loggerInfo($"清除指令历史记录表0条");
            }
            DBControl.RemoveDBConection(dBConnection);
        }
        //清除半年前的PIO历史记录表
        public void ClearPIOHistory()
        {
            DBConnection dBConnection = DBControl.GetConnection();
            string dateTime = DateTime.Now.AddMonths(-6).ToString("yyyy-MM-dd");
            string sqlString = $"DELETE FROM PORTPIOHISTORY WHERE COMPLETETIME < '{dateTime}'";
            int result = dBConnection.ExecuteUpdate(sqlString);
            if (result > 0)
            {
                LogHelper.loggerInfo($"清除PIO历史记录表完成，共删除{result}条数据");
            }
            else
            {
                LogHelper.loggerInfo($"清除PIO历史记录表0条");
            }
            DBControl.RemoveDBConection(dBConnection);
        }
        //清除半年前的稼动率记录表
        public void ClearData()
        {
            DBConnection dBConnection = DBControl.GetConnection();
            string dateTime = DateTime.Now.AddMonths(-6).ToString("yyyy-MM-dd");
            string sqlString = $"DELETE FROM DATASTATISTICS WHERE CREATETIME < '{dateTime}'";
            int result = dBConnection.ExecuteUpdate(sqlString);
            if (result > 0)
            {
                LogHelper.loggerInfo($"清除稼动率综合记录表完成，共删除{result}条数据");
            }
            else
            {
                LogHelper.loggerInfo($"清除稼动率综合历史记录表0条");
            }
            DBControl.RemoveDBConection(dBConnection);
        }
        //清除半年前的稼动率每天记录表
        public void ClearDataByDay()
        {
            DBConnection dBConnection = DBControl.GetConnection();
            string dateTime = DateTime.Now.AddMonths(-6).ToString("yyyy-MM-dd");
            string sqlString = $"DELETE FROM DATASTATISTICBYDAY WHERE DATATIME < '{dateTime}'";
            int result = dBConnection.ExecuteUpdate(sqlString);
            if (result > 0)
            {
                LogHelper.loggerInfo($"清除稼动率每天记录表完成，共删除{result}条数据");
            }
            else
            {
                LogHelper.loggerInfo($"清除稼动率每天历史记录表0条");
            }
            DBControl.RemoveDBConection(dBConnection);
        }
    }
}
